/**
*
*	\file  rtc.c
*	\author Manish Shakya 
*	\brief
*	\date  Tuesday,October 27,2009
*	\version 1.0
*
*
*/

#include "peripherals.h"
#ifdef __RTC__
/***********************************************************************************************************************/
/*!
	\Name 	rtc_set_date
	\param 	year 
	\param 	month 
	\param 	dom 
	\brief	Sets RTC with given param
*/
void rtc_set_date(uint16_t year,uint8_t month,uint8_t dom)
{
	YEAR=(year<2009)?2009:year;
	MONTH=month>12?1:month;
	DOM=dom>32?1:dom;
}

 uint32_t rtc_get_packed_date_time()
 {
 	uint32_t res;
 	res = (
		(( SEC/2 )       << 0UL  ) |
		(( MIN )         << 5UL  ) |
		(( HOUR )        << 11UL ) |
		(( DOM )        << 16UL ) |
		(( MONTH )         << 21UL ) |
		(( YEAR-1980 )   << 25UL )
	);
   return res;
 
 }
/***********************************************************************************************************************/
/*!
	\Name	RTC_SetTime 
	\param	hr	hour
	\param	min	minute
	\param	sec	sec
	\brief	Sets RTC With Current Paramter
*/
void rtc_set_time(uint8_t hr,uint8_t min,uint8_t sec)
{
										  
    HOUR=(hr>23)?0:hr;
	MIN=(min>59)?0:min;
	SEC=(sec>59)?0:sec;

}

/***********************************************************************************************************************/
/*!
	\Name	RTC_GetDate
	\param	ptrdte 
	\brief	Get Date from RTC in bin format
*/

void rtc_get_date(date_s * ptrdte)
{
    
	ptrdte->year=YEAR;
    ptrdte->month=MONTH;
    ptrdte->dom=DOM;
	
	ptrdte->year=(ptrdte->year<2009)?2009:ptrdte->year;
	ptrdte->month=ptrdte->month>12?1:ptrdte->month;
	ptrdte->dom=ptrdte->dom>32?1:ptrdte->dom;
	
	
}

/***********************************************************************************************************************/
/*!
	\Name	rtc_get_time 
	\param	ptrtme
	\brief	Get Time from RTC in bin format
*/

void rtc_get_time(time_s * ptrtme)
{
    ptrtme->hr=HOUR;
	ptrtme->min=MIN;
	ptrtme->sec=SEC;

	ptrtme->hr=(ptrtme->hr>23)?0:ptrtme->hr;
	ptrtme->min=(ptrtme->min>59)?0:ptrtme->min;
	ptrtme->sec=(ptrtme->sec>59)?0:ptrtme->sec;

}

/***********************************************************************************************************************/
/*!
	\Name	RTC_Init
	\brief	Init RTC
*/

void rtc_init()
{	    
   CCR=(1<<4)|(1<<0);
}



#endif
